/*
Libreria lettura ADS1217_16
ADC 24bit 8ch
HW TEST 1
Manuel Baldo
09/11/2009
*/


#include "p24HJ256GP206.h"
#include "ADS1217_16.h"
#include "SPI2_ADC.h"
#include "main.h"
#include "IOpin.h"  // Per funzione UsDelay

unsigned long ADC_Out = 0;
unsigned char out[24];
unsigned char count = 0;
unsigned char set_byte=0;

//****************************************************************************************************
void Init_ADS1217(void)
	{
	TRISGbits.TRISG7  =1;      				 
	TRISBbits.TRISB12 =1;
	TRISGbits.TRISG8  =0;
	TRISGbits.TRISG6  =0;
	TRISBbits.TRISB13 =0;		 
	TRISCbits.TRISC13 =1;
	TRISCbits.TRISC14 =0;
	
	CS_adc=1;
	SCLK_adc=0;
	DIN_adc=0;
	SYNC_pin=1;

	}

void Filter_ADS1217(unsigned char Data_format,unsigned char SMODE,unsigned int decimatore)
{
	unsigned char dec0;
	unsigned char mdec1;
	CS_adc = 0;
	Delay_ns(10);		
	Write_Byte(WREG|DEC0);
	Write_Byte(1);
	dec0 = decimatore & 0xFF;
	mdec1 = decimatore>>8;
	mdec1 = mdec1 & 3;

	Data_format = Data_format & 1;
	Data_format = Data_format<<6;
	mdec1 = mdec1 | Data_format;
	
	SMODE = SMODE & 3;
	SMODE = SMODE<<4;
	mdec1 = mdec1 | SMODE;

	Write_Byte(dec0); 
	Write_Byte(mdec1); 
	CS_adc = 1;

}
void PGA_ADS1217(unsigned char pga) // da 0 a 128
	{
	unsigned char set_pga;

	switch (pga)
		{
		case 1:
		set_pga = 0;		   
		break;

		case 2: 
		set_pga = 1;   
		break;

		case 4:
 		set_pga = 2;
		break;

		case 8:
  		set_pga = 3;		   
		break;

		case 16:
  		set_pga = 4;		   
		break;

		case 32:
  		set_pga = 5;		   
		break;

		case 64:
 		set_pga = 6; 		   
		break;

		case 128:
  		set_pga = 7;		   
		break;

		default:
			return;
		break;
		}

	set_pga = set_pga & 0b0000111;

	CS_adc = 0;
	Delay_ns(10);		
	Write_Byte(WREG|ACR);
	Write_Byte(0);
	set_byte = set_pga;
	Write_Byte(set_byte); 
	CS_adc = 1;
UsDelay(100000);

}
long ADS1217_OUT(unsigned char time_out)
	{
	SYNC_pin=1;
	UsDelay(10);
	SYNC_pin=1;
	Delay_ns(10);
	
	while(DRDY_adc)
		{asm("clrwdt");}
	while(!DRDY_adc)
		{asm("clrwdt");}
	while(DRDY_adc)
		{asm("clrwdt");}

	CS_adc = 0;
	Delay_ns(30); 

	Write_Byte(RDATA);
	Delay_ns(50); 
	ADC_Out = Read_bits(24);

	DIN_adc = 0; 
	CS_adc = 1;	

	return ADC_Out;
	}

void Setup_ADS1217(unsigned char setup)
{
	CS_adc = 0;
	Delay_ns(10);		
	Write_Byte(WREG|SETUP);
	Write_Byte(0);
	Write_Byte(setup);// REF Interno
	CS_adc = 1;
}

unsigned char Read_REG(unsigned char addr)
	{
	unsigned char reg=0;
	CS_adc = 0;
	Delay_ns(10);		
	Write_Byte(RREG|(addr&0x0F));
	Write_Byte(0);
	UsDelay(100);
	reg=Read_bits(8);
	CS_adc = 1;
	return reg;		
	}

void Delay_SCK(void)
	{
	float temp;
	long int cycles=1;
	temp=(float)(cycles*(FREQ/8000000));
	cycles=(long int)temp;
	while(cycles>0)
		{cycles--;}
	return;
	}

void Delay_ns(long ns) 
{  
	unsigned int i;
    for(i=0;i < ns;i++)
        asm("clrwdt");
}

void Write_Byte(unsigned char byte)
{
	unsigned char count = 0;
	for (count=0;count<8;count++) // primi byte richiesta RDATA
		{
		SCLK_adc=1;
		if((byte&(0x80>>count))>0)
			DIN_adc =1;
		else
			DIN_adc =0;
		Delay_SCK();
		SCLK_adc=0;
		Delay_SCK();
		}
	DIN_adc =0;
}
unsigned long Read_bits(unsigned char bits)
{
	long start_bit = 0;
    ADC_Out=0;
	start_bit = 1;
	start_bit = start_bit<<(bits-1);

	for (count=0;count<bits;count++) 
		{
		SCLK_adc=1;
		DIN_adc = 1; 
		Delay_SCK();
		SCLK_adc=0;
		if(DOUT_adc)
			ADC_Out = ADC_Out|(start_bit>>count);
		Delay_SCK();
		}
	DIN_adc =0;	
	return ADC_Out;

}





